package com.my.recyclerandrecycler.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.my.recyclerandrecycler.R;
import com.my.recyclerandrecycler.bean.ShoppingBean;

import java.util.List;

/**
 * 项目名称：RecyclerAndRecycler
 * 创建人：
 * 创建时间：2018/8/16 16:10
 */
public class ChildAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ShoppingBean.DataBean.ListBean> mDatas;
    private OnCheckBoxReturnListener checkBoxReturnListener;
    private int MAX_NUM = 20;
    private double currentPrice;

    public void setAllCheckBox(List<CheckBox> allCheckBox, boolean checked) {

        if (checked) { // 遍历设置全选状态 TODO
            for (int i = 0; i < allCheckBox.size(); i++) {
                allCheckBox.get(i).setChecked(true);
            }
        } else {
            for (int i = 0; i < allCheckBox.size(); i++) {
                allCheckBox.get(i).setChecked(false);
            }
        }

        for (int i = 0; i < mDatas.size(); i++) { // 设置按钮数据选择状态 TODO
            mDatas.get(i).setSelect(checked);
        }


    }

    public ChildAdapter(Context context, List<ShoppingBean.DataBean.ListBean> mDatas, OnCheckBoxReturnListener checkBoxReturnListener) {
        this.context = context;
        this.mDatas = mDatas;
        this.checkBoxReturnListener = checkBoxReturnListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_child, null);
        ChildHolder holder = new ChildHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ChildHolder) {
            Glide.with(context).load(mDatas.get(position).getGoods_image()).error(R.mipmap.ic_launcher).into(((ChildHolder) holder).imageView);

            ((ChildHolder) holder).tvName.setText(mDatas.get(position).getGoods_name()); // 商品名称
            ((ChildHolder) holder).tvPrice.setText(mDatas.get(position).getGoods_price() + "￥"); // 商品价格
            ((ChildHolder) holder).tvNum.setText(""+mDatas.get(position).getGoods_num()); // 数量
            // 设置全选所需的holder.checkBox
            checkBoxReturnListener.onCheckBox(((ChildHolder) holder).checkBox);

            // 添加按钮
            ((ChildHolder) holder).ivAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkBoxReturnListener.addNum(position);
                    Log.i("mwb", "add:" + position);

                    int num = mDatas.get(position).getGoods_num() + 1;
                    ((ChildHolder) holder).tvNum.setText("" + num);
                    mDatas.get(position).setGoods_num(num);
                    setNumText(holder, num, position);

                    // TODO 判断当前的checkBox是否选中了
                    if (mDatas.get(position).isSelect()) {
                        int currentNum = mDatas.get(position).getGoods_num();
                        currentPrice = currentNum * mDatas.get(position).getGoods_price();
                        checkBoxReturnListener.currentPrice(currentPrice);
                    }

                }
            });

            // 减去按钮
            ((ChildHolder) holder).ivCut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkBoxReturnListener.cutNum(position);
                    Log.i("mwb", "cut:" + position);
                    int num = mDatas.get(position).getGoods_num() - 1;
                    mDatas.get(position).setGoods_num(num);
                    setNumText(holder, num, position);
                }
            });
        }
    }

    /**
     * 设置数量文字
     *
     * @param holder
     * @param num
     * @param position
     */
    private void setNumText(RecyclerView.ViewHolder holder, int num, int position) {
        if (holder instanceof ChildHolder) {
            if (num <= 1) {
                num = 1;
                ((ChildHolder) holder).tvNum.setText("0" + num);
            } else if (num < 10) {
                ((ChildHolder) holder).tvNum.setText("0" + num);
            } else if (num >= MAX_NUM) {
                num = MAX_NUM;
                ((ChildHolder) holder).tvNum.setText("" + num);
            } else {
                ((ChildHolder) holder).tvNum.setText("" + num);
            }
        }
        mDatas.get(position).setGoods_num(num);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    private class ChildHolder extends RecyclerView.ViewHolder {
        private CheckBox checkBox;
        private ImageView imageView;
        private TextView tvName;
        private TextView tvPrice;
        private ImageView ivCut;
        private ImageView ivAdd;
        private TextView tvNum;

        public ChildHolder(View itemView) {
            super(itemView);
            checkBox = itemView.findViewById(R.id.item_child_cb);
            imageView = itemView.findViewById(R.id.item_child_img);
            tvName = itemView.findViewById(R.id.item_child_name_tv);
            tvPrice = itemView.findViewById(R.id.item_child_price_tv);
            ivCut = itemView.findViewById(R.id.item_child_cut_img);
            ivAdd = itemView.findViewById(R.id.item_child_add_img);
            tvNum = itemView.findViewById(R.id.item_child_num_tv);
        }
    }

    public interface OnCheckBoxReturnListener {
        /**
         * 全选
         *
         * @param checkBox
         */
        void onCheckBox(CheckBox checkBox);

        /**
         * 增加数量
         *
         * @param position
         */
        void addNum(int position);

        /**
         * 减少数量
         *
         * @param position
         */
        void cutNum(int position);

        /**
         * 返回价格
         *
         * @param price
         */
        void currentPrice(double price);
    }

}
